package com.jingdianjichi.auth.application.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.jingdianjichi.auth.application.convent.AuthUserConvent;
import com.jingdianjichi.auth.application.dto.AuthUserDto;
import com.jingdianjichi.auth.common.entity.Result;
import com.jingdianjichi.auth.domain.entity.AuthUserBo;
import com.jingdianjichi.auth.domain.service.AuthUserDomainService;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;


@RestController
@RequestMapping("/user/")
@Slf4j
public class UserController {


    @Resource
    AuthUserDomainService authUserDomainService;

    /**
     * 用户注册
     * @param authUserDto
     * @return
     */
    @PostMapping("register")
    public Result Register(@RequestBody AuthUserDto authUserDto) {

        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.register.dto,{}", JSON.toJSONString(authUserDto));
            }

            Preconditions.checkNotNull(authUserDto.getUserName(), "用户名称不能为空");
            Preconditions.checkArgument(!StringUtils.isEmpty(authUserDto.getEmail()), "电子邮箱不能为空");
            Preconditions.checkNotNull(authUserDto.getPassword(), "用户密码不能为空");


            AuthUserBo authUserBo = AuthUserConvent.INSERT.conventDtoToBo(authUserDto);

            authUserDomainService.add(authUserBo);

            return Result.ok();
        } catch (Exception e) {
            log.error("UserController.register.error,{}", e.getMessage(), e);
            return Result.fail(e.getMessage());
        }

    }


    /**
     * 用户信息修改
     * @param
     * @param
     * @return
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthUserDto authUserDto) {

        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto,{}", JSON.toJSONString(authUserDto));
            }

            Preconditions.checkNotNull(authUserDto.getId(), "用户id不能为空");


            AuthUserBo authUserBo = AuthUserConvent.INSERT.conventDtoToBo(authUserDto);

            Boolean update = authUserDomainService.update(authUserBo);

            return Result.ok(update);
        } catch (Exception e) {
            log.error("UserController.update.error,{}", e.getMessage(), e);
            return Result.fail(e.getMessage()+"用户信息更新失败");
        }

    }

    /**
     * 用户启用和禁用
     * @param
     * @param
     * @return
     */
    @PostMapping("updateStatus")
    public Result<Boolean> updateStatus(@RequestBody AuthUserDto authUserDto) {

        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.update.dto,{}", JSON.toJSONString(authUserDto));
            }

            Preconditions.checkNotNull(authUserDto.getUserName(), "用户名称不能为空");
            Preconditions.checkNotNull(authUserDto.getStatus(), "用户状态不能为空");


            AuthUserBo authUserBo = AuthUserConvent.INSERT.conventDtoToBo(authUserDto);

            Boolean update = authUserDomainService.update(authUserBo);

            return Result.ok(update);
        } catch (Exception e) {
            log.error("UserController.update.error,{}", e.getMessage(), e);
            return Result.fail(e.getMessage()+"用户启用禁用失败");
        }

    }

    /**
     * 用户信息删除
     * @param
     * @param
     * @return
     */
    @PostMapping("delect")
    public Result<Boolean> delect(@RequestBody AuthUserDto authUserDto) {

        try {
            if (log.isInfoEnabled()) {
                log.info("UserController.delect.dto,{}", JSON.toJSONString(authUserDto));
            }

            Preconditions.checkNotNull(authUserDto.getId(), "用户id不能为空");


            AuthUserBo authUserBo = AuthUserConvent.INSERT.conventDtoToBo(authUserDto);

            Boolean update = authUserDomainService.delect(authUserBo);

            return Result.ok(update);
        } catch (Exception e) {
            log.error("UserController.delect.error,{}", e.getMessage(), e);
            return Result.fail(e.getMessage()+"用户信息更新失败");
        }

    }



    // 测试登录，浏览器访问： http://localhost:8081/user/doLogin?username=zhang&password=123456
    @RequestMapping("doLogin")
    public String doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if ("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            String tokenValue = StpUtil.getTokenValue();
            System.out.println("token------------------"+tokenValue);
            return "登录成功";
        }
        return "登录失败";
    }

    // 查询登录状态，浏览器访问： http://localhost:8081/user/isLogin
    @RequestMapping("isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

}


